using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SheLife.Application.Interfaces;
using SheLife.Domain.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace SheLife.Api.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class SkincareRecordController : ControllerBase
    {
        private readonly ISkincareRecordService _skincareRecordService;

        public SkincareRecordController(ISkincareRecordService skincareRecordService)
        {
            _skincareRecordService = skincareRecordService;
        }

        [HttpGet("{id}")]
        public async Task<ActionResult<SkincareRecord>> GetSkincareRecord(int id)
        {
            var record = await _skincareRecordService.GetSkincareRecordByIdAsync(id);
            if (record == null)
            {
                return NotFound();
            }
            return record;
        }

        [HttpGet("skincare/{skincareId}")]
        public async Task<ActionResult<IEnumerable<SkincareRecord>>> GetSkincareRecordsBySkincareId(int skincareId)
        {
            var records = await _skincareRecordService.GetSkincareRecordsBySkincareIdAsync(skincareId);
            return Ok(records);
        }

        [HttpGet("user/{userId}")]
        public async Task<ActionResult<IEnumerable<SkincareRecord>>> GetSkincareRecordsByUserId(int userId)
        {
            var records = await _skincareRecordService.GetSkincareRecordsByUserIdAsync(userId);
            return Ok(records);
        }

        [HttpGet("user/{userId}/daterange")]
        public async Task<ActionResult<IEnumerable<SkincareRecord>>> GetSkincareRecordsByDateRange(
            int userId, 
            [FromQuery] DateTime startDate, 
            [FromQuery] DateTime endDate)
        {
            var records = await _skincareRecordService.GetSkincareRecordsByDateRangeAsync(userId, startDate, endDate);
            return Ok(records);
        }

        [HttpGet("user/{userId}/monthly/{year}/{month}")]
        public async Task<ActionResult<object>> GetMonthlyStats(int userId, int year, int month)
        {
            var stats = await _skincareRecordService.GetMonthlyStatsAsync(userId, year, month);
            return Ok(stats);
        }

        [HttpGet("user/{userId}/productusage")]
        public async Task<ActionResult<object>> GetProductUsageStats(int userId)
        {
            var stats = await _skincareRecordService.GetProductUsageStatsAsync(userId);
            return Ok(stats);
        }

        [HttpPost]
        public async Task<ActionResult<SkincareRecord>> CreateSkincareRecord(SkincareRecord record)
        {
            var createdRecord = await _skincareRecordService.CreateSkincareRecordAsync(record);
            return CreatedAtAction(nameof(GetSkincareRecord), new { id = createdRecord.Id }, createdRecord);
        }

        [HttpPost("withproducts")]
        public async Task<ActionResult<SkincareRecord>> CreateSkincareRecordWithProducts([FromBody] SkincareRecordWithProductsRequest request)
        {
            var createdRecord = await _skincareRecordService.CreateSkincareRecordWithProductsAsync(
                request.Record, 
                request.ProductIds);
            return CreatedAtAction(nameof(GetSkincareRecord), new { id = createdRecord.Id }, createdRecord);
        }

        [HttpPut("{id}")]
        public async Task<IActionResult> UpdateSkincareRecord(int id, SkincareRecord record)
        {
            if (id != record.Id)
            {
                return BadRequest();
            }

            await _skincareRecordService.UpdateSkincareRecordAsync(record);
            return NoContent();
        }

        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteSkincareRecord(int id)
        {
            await _skincareRecordService.DeleteSkincareRecordAsync(id);
            return NoContent();
        }
    }

    public class SkincareRecordWithProductsRequest
    {
        public SkincareRecord Record { get; set; }
        public List<int> ProductIds { get; set; }
    }
}